using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.IFilter
{
    [Serializable]
    public class FileContentSearchInstruction:InstructionBase
    {
        private List<string> _SrcFilePaths;

        public List<string> SourceFilePaths
        {
            get { return _SrcFilePaths; }
            set { _SrcFilePaths = value; }
        }

        private SearchOption _SearchOption;

        public SearchOption SearchOption
        {
            get { return _SearchOption; }
            set { _SearchOption = value; }
        }

        private string _FileNamePattern;

        public string FileNamePattern
        {
            get { return _FileNamePattern; }
            set { _FileNamePattern = value; }
        }

        private StringMatchType _StringMatchType;

        public StringMatchType StringMatchOption
        {
            get { return _StringMatchType; }
            set { _StringMatchType = value; }
        }
	
        public FileContentSearchInstruction()
            : base(Guid.Empty, Guid.Empty)
        {
            this.SourceFilePaths = null;
            this._SearchOption = System.IO.SearchOption.TopDirectoryOnly;
            this._FileNamePattern = string.Empty;
            this._StringMatchType = StringMatchType.ExactMatch;
        }

        public FileContentSearchInstruction(Guid componentID, Guid jobTicketID, 
            List<string> srcFiles, string fileNamePattern, 
            SearchOption searchOp, StringMatchType stringMatchOp)
            : base(componentID, jobTicketID)
        {
            this.SourceFilePaths = srcFiles;
            this._FileNamePattern = fileNamePattern;
            this._SearchOption = searchOp;
            this._StringMatchType = stringMatchOp;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());
            
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNamePattern", this.FileNamePattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SearchOption", ((int) this.SearchOption).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"StringMatchOption",((int)this.StringMatchOption).ToString());

            XmlNode srcFileRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "SourceFiles");
            if(this.SourceFilePaths !=null && this.SourceFilePaths.Count>0)
            {
                foreach(string filePath in this.SourceFilePaths)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, srcFileRootNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fileNode, "FilePath", filePath);
                }
            }

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileContentSearchInstruction instruction = new FileContentSearchInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.FileNamePattern = xNode.Attributes.GetNamedItem("FileNamePattern").Value;
                instruction.SearchOption = (SearchOption) int.Parse(xNode.Attributes.GetNamedItem("SearchOption").Value);
                instruction.StringMatchOption =
                    (StringMatchType) int.Parse(xNode.Attributes.GetNamedItem("StringMatchOption").Value);
                
                instruction.SourceFilePaths = null;
                XmlNodeList fileNodes = xNode.SelectNodes("SourceFiles/File");
                if(fileNodes!=null && fileNodes.Count>0)
                {
                    instruction.SourceFilePaths=new List<string>();
                    foreach(XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        instruction.SourceFilePaths.Add(filePath);
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}